package mylittleinvestigations.GameUi;

import mylittleinvestigations.Events.ImageButtonEventProvider;
import mylittleinvestigations.MouseHelper;
import mylittleinvestigations.PersistentThreads.RenderThread;
import org.dawnmew.jpurewave.SoundSystem;
import org.newdawn.slick.*;
import org.newdawn.slick.geom.Rectangle;

public class ImageButton
{
    private Image mouseOffImage;
    private Image mouseOverImage;
    private Image mouseDownImage;

    private boolean isMouseOver;
    private boolean isMouseDown;

    private String clickSoundEffect = "ButtonClick1";

    public ImageButton(Image mouseOffImage, Image mouseOverImage, Image mouseDownImage, int xPosition, int yPosition)
    {
        this.mouseOffImage = mouseOffImage;
        this.mouseOverImage = mouseOverImage;
        this.mouseDownImage = mouseDownImage;
        
        this.xPosition = xPosition;
        this.yPosition = yPosition;

        this.Reset();
    }

    private int xPosition;

    public int getXPosition() { return this.xPosition; }
    public void setXPosition(int xPosition) { this.xPosition = xPosition; }

    private int yPosition;

    public int getYPosition() { return this.yPosition; }
    public void setYPosition(int yPosition) { this.yPosition = yPosition; }

    public String getClickSoundEffect()
    {
        return this.clickSoundEffect;
    }

    public void Update(GameContainer gc,int delta)
    {
        this.isMouseOver = false;
        this.isMouseDown = false;
        
        Image currentImage = this.getCurrentImage();
        
        Rectangle positionRect = new Rectangle(this.getXPosition(), this.getYPosition(), currentImage.getWidth(), currentImage.getHeight());

        if (MouseHelper.ClickedOnRect(positionRect))
        {
            this.OnClicked();
        }

        if (MouseHelper.MouseDownOnRect(positionRect))
        {
            this.isMouseDown = true;
        }
        else if (MouseHelper.MouseOverRect(positionRect))
        {
            this.isMouseOver = true;
        }

        if (this.isMouseOver || this.isMouseDown)
        {
            MouseHelper.SetToHandCursor();
        }
    }

    public void Draw(GameContainer gc, Graphics g)
    {
        RenderThread.getInstance().AddImageToRenderQueue(this.getCurrentImage(), this.getXPosition(), this.getYPosition());
    }

    public void Reset()
    {
        this.isMouseOver = false;
        this.isMouseDown = false;
    }
    
    private Image getCurrentImage()
    {
        if (this.isMouseDown)
        {
            return this.mouseDownImage;
        }
        else if (this.isMouseOver)
        {
            return this.mouseOverImage;
        }
        else
        {
            return this.mouseOffImage;
        }
    }

    private void OnClicked()
    {
        ImageButtonEventProvider.RaiseClicked(this);
        SoundSystem.getInstance().playSound(getClickSoundEffect());
    }
}
